package appname.example.com.testmvvm;

import android.databinding.DataBindingUtil;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.v7.util.DiffUtil;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;

import appname.example.com.testmvvm.databinding.ActivityMainBinding;
import appname.example.com.testmvvm.databinding.ItemViewBinding;

public class MainCtrl {

    public ActivityMainBinding binding;

    private ArrayList<Testbean> datas = new ArrayList<>();


    private ArrayList<Testbean> newDatas = new ArrayList<>();
    private final MyAdapter myAdapter;

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            DiffUtil.DiffResult diffResult = (DiffUtil.DiffResult) msg.obj;
            diffResult.dispatchUpdatesTo(myAdapter);
            datas = newDatas;
            myAdapter.setData(datas);
        }
    };


    public MainCtrl(ActivityMainBinding binding) {
        this.binding = binding;
        intitdata();
        binding.recyclerview.setLayoutManager(new LinearLayoutManager(binding.getRoot().getContext()));
        myAdapter = new MyAdapter();
        myAdapter.setData(datas);
        binding.recyclerview.setAdapter(myAdapter);
    }


    public void changeData(View view) {
        newDatas.addAll(datas);
        Testbean testbean = new Testbean();
        testbean.setName("小白1");
        testbean.setName1("小黑2");
        testbean.setName2("张三1");
        testbean.setName3("李四2");
        newDatas.add(testbean);
        newDatas.get(3).setName1("haha");
        newDatas.get(2).setName2("hehe");
        newDatas.get(5).setName2("666");
        Testbean testbean1 = newDatas.get(1);
        newDatas.remove(testbean1);

        // 这个可能是个耗时的操作，需要在子线程中执行。

        new Thread(() -> {

            Message message = handler.obtainMessage();
            DiffUtil.DiffResult diffResult = DiffUtil.calculateDiff(new MyDeffutilCallback(datas, newDatas), false);
            message.obj = diffResult;
            handler.sendMessage(message);

        }).start();
//        diffResult.dispatchUpdatesTo(myAdapter);
//
//        datas = newDatas;
//        myAdapter.setData(datas);


    }


    public void intitdata() {
        for (int i = 0; i < 100; i++) {
            Testbean bean1 = new Testbean();
            bean1.setName("0001");
            bean1.setName1("0002");
            bean1.setName2("0003");
            bean1.setName3("0005");
            datas.add(bean1);
        }
    }




    public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyHolder> {

        ArrayList<Testbean> datas = new ArrayList<>();

        @NonNull
        @Override
        public MyHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
            ItemViewBinding binding = DataBindingUtil.inflate(
                    LayoutInflater.from(viewGroup.getContext()),
                    R.layout.item_view, viewGroup,
                    false);
            return new MyHolder(binding);
        }

        @Override
        public void onBindViewHolder(@NonNull MyHolder myHolder, int i) {
            myHolder.setData(datas.get(i));
        }

        @Override
        public int getItemCount() {
            return datas.size();
        }

        public void setData(ArrayList<Testbean> datas) {
            this.datas = datas;
        }

        class MyHolder extends RecyclerView.ViewHolder {
            public ItemViewBinding binding;

            public MyHolder(ItemViewBinding binding) {
                super(binding.getRoot());
                this.binding = binding;
            }

            public void setData(Testbean testbean) {
                binding.setVariable(BR.ctrl, testbean);
            }
        }
    }


}
